//
//  MoveConstructorMoveAssignment.hpp
//  learnC++
//
//  Created by liuzhibing on 2018/6/26.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#ifndef MoveConstructorMoveAssignment_hpp
#define MoveConstructorMoveAssignment_hpp

#include <stdio.h>
#include <iostream>


template <class T>
class AutoPtr3 {
    T *m_ptr;
    
public:
    
    AutoPtr3(T *ptr=nullptr):m_ptr(ptr){

    }
    
    ~AutoPtr3(){

        delete m_ptr;
    }
    
    //复制构造函数
    AutoPtr3(const AutoPtr3 &p){
        m_ptr = new T;
        *m_ptr = *p.m_ptr;
    }
    
    AutoPtr3 &operator=(const AutoPtr3 &p){
        if (&p == this) {
            return *this;
        }

        delete m_ptr;

        m_ptr = new T;
        *m_ptr = *p.m_ptr;
        return *this;
    }
    
    T &operator*(){return *m_ptr;}
    T *operator->(){return m_ptr;}
    bool isNull() const {return m_ptr == nullptr;}
};

class Resource3
{
public:
    Resource3() { std::cout << "Resource acquired\n"; }
    ~Resource3() { std::cout << "Resource destroyed\n"; }
};

AutoPtr3<Resource3> generateResource();


//你的编译器正在消除拷贝构造函数，所以在你的情况下它没有做任何事情。 但是其他编译器可能不会隐藏拷贝构造函数
/*
 
 AutoPtr3<Resource3> ap;
 ap = generateResource();
 
 */


#endif /* MoveConstructorMoveAssignment_hpp */
